import PageBody from '@/components/school/pageBody';
import diyJson from '@/config/diy.js';
import {BASE_CONFIG} from "@/config/app";
import {mapGetters} from "vuex";
import OrderList from '@/components/school/orderList';

const app = getApp();

export const mixins = {
  name: "ChatMsg",
  components: {
    PageBody, OrderList
  },
  computed: mapGetters(['userInfo']),
  props: {
    params: {
      type: Object,
      default: function () {
        return {
          refId: "CUSTOMER",
          title:"客服聊天",
          chatType: "CUSTOMER"
        }
      }
    }
  },
  data() {
    return {
      themConfig: diyJson.commonStyle,
      rules: [],
      invite: {},
      tagStyle: {
        img: 'width:100%;display:block;',
        table: 'width:100%',
        video: 'width:100%'
      },
      showLoading: true,
      chatMsgList: [
        {
          "groupId": "1857775427126935553",
          "memberId": "1001020123",
          "memberName": "接单员",
          "memberAvatar": "http://127.0.0.1",
          "msgType": 4,
          "content": {
            "memberId": "1002",
            "takerMemberId": "1001020123",
            "photos": [
              "http://127.0.0.1"
            ],
            "content": "帮忙拿快递",
            "actionAddress": "南门",
            "destination": "男寝12栋",
            "orderNo": "1857582813012848640",
            "orderName": "代拿代办",
            "createTime": "2024-10-11 00:00:00"
          },
          "createTime": '2024-11-16 19:29:11'
        },
        {
          "groupId": 1857775427126935553,
          "memberId": 1001020123,
          "memberName": "接单员",
          "memberAvatar": "http://127.0.0.1",
          "msgType": 1,
          "content": "送到寝室楼下了；上不去，麻烦下来拿一下",
          "createTime": '2024-11-16 19:39:11'
        }
      ],
      content: ``,
      orderInfo: {},
      pageBody: {
        background: 'white',
        // #ifdef MP
        pageHeight: '100%',
        // #endif
        // #ifdef H5
        pageHeight: app.globalData.windows.pageBody,
        // #endif
      },
      sockectClient: undefined,
    }
  },
  onShow() {


  },
  created(){
    console.log("websocketUrl111", BASE_CONFIG.websocketUrl);
    console.log("this.params",this.params)
  },
  mounted() {
  },
  methods: {
    initWebSocket() {
      if (typeof (WebSocket) == "undefined") {
        console.log("您的浏览器不支持WebSocket");
      } else {
        // 'ws://127.0.0.1:39307/{userId}/{refId}/{chatType}/{tenantId}',
        let url = BASE_CONFIG.websocketUrl;
        url = url.replace("{userId}", this.userInfo.id)
        .replace("{refId}", this.params.refId)
        .replace("{chatType}", this.params.chatType)
        .replace("{tenantId}", BASE_CONFIG.tenantId);

        console.log("websocketUrl", url)

        this.sockectClient = new WebSocket(url);

        this.sockectClient.onopen = function () {
          console.log("Socket 已打开");
        }

        this.sockectClient.onmessage = function (msg) {
          let res = JSON.parse(msg.data);
          console.log("接收到消息", res)
        }

        //关闭事件
        this.sockectClient.onclose = function () {
          console.log("Socket已关闭");
        };

        //发生了错误事件
        this.sockectClient.onerror = function () {
          console.log("Socket发生了错误");
        }
      }
    }
  }
}
